package com.mineoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mineoj.common.BaseResponse;
import com.mineoj.common.ErrorCode;
import com.mineoj.common.ResultUtils;
import com.mineoj.exception.BusinessException;
import com.mineoj.exception.ThrowUtils;
import com.mineoj.model.dto.questionlist.QuestionListQueryRequest;
import com.mineoj.model.dto.questionlistfavour.QuestionListFavourAddRequest;
import com.mineoj.model.dto.questionlistfavour.QuestionListFavourQueryRequest;
import com.mineoj.model.entity.QuestionList;
import com.mineoj.model.entity.User;
import com.mineoj.model.vo.QuestionListVO;
import com.mineoj.service.QuestionListFavourService;
import com.mineoj.service.QuestionListService;
import com.mineoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题单收藏接口
 *
 
 */
@RestController
@RequestMapping("/questionList_favour")
@Slf4j
public class QuestionListFavourController {

    @Resource
    private QuestionListFavourService questionListFavourService;

    @Resource
    private QuestionListService questionListService;

    @Resource
    private UserService userService;

    /**
     * 收藏 / 取消收藏
     *
     * @param questionListFavourAddRequest
     * @param request
     * @return resultNum 收藏变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doQuestionListFavour(@RequestBody QuestionListFavourAddRequest questionListFavourAddRequest,
            HttpServletRequest request) {
        if (questionListFavourAddRequest == null || questionListFavourAddRequest.getQuestionListId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userService.getLoginUser(request);
        long questionListId = questionListFavourAddRequest.getQuestionListId();
        int result = questionListFavourService.doQuestionListFavour(questionListId, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 获取我收藏的题单列表
     *
     * @param questionListQueryRequest
     * @param request
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<QuestionListVO>> listMyFavourQuestionListByPage(@RequestBody QuestionListQueryRequest questionListQueryRequest,
            HttpServletRequest request) {
        if (questionListQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long current = questionListQueryRequest.getCurrent();
        long size = questionListQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionList> questionListPage = questionListFavourService.listFavourQuestionListByPage(new Page<>(current, size),
                questionListService.getQueryWrapper(questionListQueryRequest), loginUser.getId());
        return ResultUtils.success(questionListService.getQuestionListVOPage(questionListPage, request));
    }

    /**
     * 获取用户收藏的题单列表
     *
     * @param questionListFavourQueryRequest
     * @param request
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionListVO>> listFavourQuestionListByPage(@RequestBody QuestionListFavourQueryRequest questionListFavourQueryRequest,
            HttpServletRequest request) {
        if (questionListFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = questionListFavourQueryRequest.getCurrent();
        long size = questionListFavourQueryRequest.getPageSize();
        Long userId = questionListFavourQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<QuestionList> questionListPage = questionListFavourService.listFavourQuestionListByPage(new Page<>(current, size),
                questionListService.getQueryWrapper(questionListFavourQueryRequest.getQuestionListQueryRequest()), userId);
        return ResultUtils.success(questionListService.getQuestionListVOPage(questionListPage, request));
    }
}
